﻿
@{
    ViewBag.Title = "添加油机";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
<link rel="stylesheet" href="~/Content/css/easyui.css" />
<link rel="stylesheet" href="~/Content/css/weui.min.css">
<link rel="stylesheet" href="~/Content/css/jquery-weui.min.css">
<script src="~/Content/script/jquery-1.11.0.min.js"></script>
<script src="~/Content/script/jquery-weui-1.2.1.min.js"></script>
<script src="~/Content/script/jquery.easyui.min.js"></script>
<script src="~/Content/script/jquery.easyui-1.4.3.min.js"></script>

<div class="weui-cells weui-cells_form" style="bottom: 20px;">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">油机编号:</label></div>
        <div class="weui-cell__bd">
            <input id="GensetNum" class="weui-input" type="text" placeholder="扫描油机二维码">
        </div>
        <div id="gScan" class="weui-btn weui-btn_mini weui-btn_primary">扫描</div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">采集器ID:</label></div>
        <div class="weui-cell__bd">
            <input id="DTUID" class="weui-input" type="text" placeholder="扫描模块二维码">
        </div>
        <div id="cScan" class="weui-btn weui-btn_mini weui-btn_primary">扫描</div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">SIM卡号:</label></div>
        <div class="weui-cell__bd">
            <input id="DTUSIM" class="weui-input" type="text" placeholder="SIM卡号">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">所属公司:</label></div>
        <div class="weui-cell__bd">
            <input class="easyui-combotree" id="CompanyCode" name="CompanyCode" @*data-options="editable:false"*@>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">采集器厂家:</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="FACCODE" type="text" placeholder="选择采集器厂家">
        </div>
    </div>



</div>
<div class="weui-cells__tips">*以上油机信息均为必填项</div>

@*<div class="ui-btn-wrap" id="footer">
        <button class="ui-btn-primary">
            保存
        </button>
    </div>*@

<div class="weui-btn-area">
    <div id="submit" class="weui-btn weui-btn_primary">添加油机</div>
</div>



<script src="~/Content/script/jweixin-1.4.0.js"></script>
<script>



    //微信jdk
    $.ajax({
        url: '/WeiXin/JsapiTicketSignature?fullUrl=' + window.location.href + '',
        type: 'get',
        //beforeSend: function () {
        //    $.showLoading("加载中...");
        //},
        success: function (data) {
            //$.hideLoading();
            var result = eval(data);
            // wx.config成功后才会触发wx.ready
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: result.appId, // 必填，公众号的唯一标识
                timestamp: result.timestamp, // 必填，生成签名的时间戳
                nonceStr: result.nonceStr, // 必填，生成签名的随机串
                signature: result.signature,// 必填，签名
                jsApiList: ["scanQRCode","getLocation"] // 必填，需要使用的JS接口列表
            });
        }
    });
    wx.ready(function () {
        //油机扫描事件
        $("#gScan").click(function () {
            wx.scanQRCode({
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: function (res) {
                    var gScanCode = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                    $("#GensetNum").val(gScanCode);
                }
            });
        });
        //采集器扫描事件
        $("#cScan").click(function () {
            wx.scanQRCode({
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: function (res) {
                    var cScanCode = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                    $("#DTUID").val(cScanCode);
                }
            });
        });

    });


    var GensetNum, DTUID, DTUSIM, CompanyCode, FACCODE;

    //添加事件
    $("#submit").click(function () {
        //checkSubmit({
        //    GensetNum: $("#GensetNum").val(),
        //    DTUID: $("#DTUID").val(),
        //    DTUSIM: $("#DTUSIM").val(),
        //    CompanyCode: $("#CompanyCode").combotree("getValue"),
        //    FACCODE: $("#FACCODE").data('values')
        //});
        var data = {
            GensetNum: $("#GensetNum").val(),
            DTUID: $("#DTUID").val(),
            DTUSIM: $("#DTUSIM").val(),
            CompanyCode: $("#CompanyCode").combotree("getValue"),
            FACCODE: $("#FACCODE").data('values')
        }
//
        if (data.GensetNum == "" || data.DTUID == "" || data.DTUSIM == "" || data.CompanyCode == "" || typeof (data.FACCODE) == 'undefined') {
            $.alert("请输入完整信息", "提示");
            return;
        }
        var patt1 = new RegExp(/\s+/g);
        if (patt1.test(data.DTUID)) {
            $.alert("采集器ID有空格", "提示");
            return;
        }
        $.confirm("是否执行此操作?", "提示", function () {
            $.ajax({
                url: '/api/Generator/AddGen?GensetNum=' + $("#GensetNum").val() + '&DTUID=' + $("#DTUID").val() + '&DTUSIM=' + $("#DTUSIM").val() + '&CompanyCode=' + $("#CompanyCode").combotree("getValue") + '&FACCODE=' + $("#FACCODE").data('values') + '',
                //data: {
                //    GensetNum: $("#GensetNum").val(),
                //    DTUID: $("#DTUID").val(),
                //    DTUSIM: $("#DTUSIM").val(),
                //    CompanyCode: $("#CompanyCode").combotree("getValue"),
                //    FACCODE: $("#FACCODE").data('values')
                //},
                type: 'get',
                beforeSend: function () {
                    $.showLoading("提交中...");
                }, success: function (res) {
                    $.hideLoading();
                    // var resd= JSON.parse(res);
                    var resd = res.Data;
                    console.log(res)
                    if (resd.code===1) {
                        $.toast("添加成功");
                        setTimeout(function () {
                            location.href = "/Generator/Index";
                        }, 1 * 1000);
                    } else {
                        $.alert(resd.result);
                    }
                }
            })
        }, function () {

        })
    })

    //检查提交数据
    function checkSubmit(data) {
        if (data.GensetNum == "" || data.DTUID == "" || data.DTUSIM == "" || data.CompanyCode == "" || typeof(data.FACCODE) == 'undefined') {
            $.alert("请输入完整信息", "提示");
            return;
        }
        var patt1 = new RegExp(/\s+/g);
        if (patt1.test(data.DTUID)) {
            $.alert("采集器ID有空格", "提示");
            return;
        }
    }

    //加载归属公司
    bindCompanyTree('#CompanyCode', true);

    //选择采集器厂家
    $("#FACCODE").select({
        title: "选择采集器厂家",
        items: [
            {
                title: "中移物联",
                value:"ZYWLW",
            },
            {
                title: "润建",
                value: "RJTX",
            },
            {
                title: "宗申",
                value: "CQZS",
            }
        ]
    })

 @** 绑定公司树下拉列表
 * @param {} control             #控件ID
 * @param {} bSelectFirstNode    是否选中绑定后的第一项
 * @returns {}*@

    function bindCompanyTree(control, bSelectFirstNode, onSuccess, onSelectFun) {
        bindCombotreeFromApi(
            control, //combotree-ID
           // '/Generator/GetCompanys', //api
            '/api/info/GetCompanys', //api
            'CompanyCode', //data的ID字段
            'ParentCode',     //data的父ID字段
            'CompanyName',//data中用于显示的Text字段
            onSelectFun, onSuccess, bSelectFirstNode
            );
    }
    function bindCombotreeFromApi(control, api, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal, bSync) {
        try {
            $.ajax({
                url: api,
                type: 'get',
                success: function (d) {
                    var array = JSON.parse(d.Data)
                    bindCombotreeFromData(control, array, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal);
                }
            });
        } catch (e) {
            alert(e.message);
        }
    }


    function bindCombotreeFromData(control, data, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal) {
        try {
            data = data || [];//避免data=null引起异常
            data = copyProperty(data, [idField, pidFiled, textField], ['id', 'pid', 'text'], true); //转化为easyui - tree需要的id属性
            if (bLocal && lan) { //本地化处理
                lan.localObjects(data, 'text');
            }
            data = toTreeData(data, "id", "pid", "children"); //转化为easyui-tree需要的json-tree格式
            //data = copyProperty(data, [  textField], [ 'text'], true); //转化为easyui - tree需要的id属性
            //data = toTreeData(data, idField, pidFiled, "children"); //转化为easyui-tree需要的json-tree格式

            $(control)
               .combotree({
                   data: data
               });

            if (typeof (selectFun) != 'undefined' && selectFun != null)
                $(control)
                   .combotree({
                       onSelect: selectFun
                   });
            if (typeof (onLoadSuccess) != 'undefined' && onLoadSuccess != null)
                $(control)
                   .combotree({
                       onLoadSuccess: onLoadSuccess
                   });
            if (bSelectFirstNode && data && data[0]) {
                var firstValue = data[0][idField];
                $(control).combotree("setValue", firstValue);
            }
        } catch (err) {
            alert(err.message);
        }
    }

    /*
    json格式转树状结构
    param   {json}      json数据
    param   {String}    id的字符串
    param   {String}    父id的字符串
    param   {String}    children的字符串
    */
    toTreeData = function (a, idStr, pidStr, childrenStr) {
        var r = [], hash = {}, len = (a || []).length;
        for (var i = 0; i < len; i++) {
            hash[a[i][idStr]] = a[i];
        }
        for (var j = 0; j < len; j++) {
            var aVal = a[j], hashVP = hash[aVal[pidStr]];
            if (hashVP) {
                !hashVP[childrenStr] && (hashVP[childrenStr] = []);
                hashVP[childrenStr].push(aVal);
            } else {
                r.push(aVal);
            }
        }
        return r;
    };

    copyProperty = function (obj, sourcePropertyName, newPropertyName, overWrite) {
        if (obj instanceof Array || Object.prototype.toString.call(obj) === "[object Array]") {
            for (var k in obj)
                copyProperty(obj[k], sourcePropertyName, newPropertyName);
        }
        else if (typeof obj === 'object') {
            if (sourcePropertyName instanceof Array || Object.prototype.toString.call(sourcePropertyName) === "[object Array]") {
                for (var i in sourcePropertyName) {
                    copyProperty(obj, sourcePropertyName[i], newPropertyName[i]);
                }
            }
            else if (typeof sourcePropertyName === 'string') {
                if ((obj[newPropertyName] && overWrite) || (!obj[newPropertyName]))
                    obj[newPropertyName] = obj[sourcePropertyName];
            }
        }
        return obj;
    };

</script>